<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
	    .errorText
	    {
	        color:red;
	    }
    </style>
</head>

<body>
	<form id="form1" method="post" onkeydown="if(event.keyCode==13){return false;}">
		<p style="margin-left: 20px;"><label>请输入类别名称：</label></p>
		<p style="margin-left: 20px;">
			<input id="textbox1"  name="name" class="mini-textbox" errorMode="none" onvalidation="checkValidation" requiredErrorText="类别名称不能为空" required="true" vtype="rangeLength:0,50" style="width: 200px;" />
		</p>
		<p style="margin-left: 20px;"><span id="name_error" class="errorText" ></span></p>
		<input name="pid" class="mini-hidden" />
	</form>
    <p style="text-align: center;margin-top: 55px">
    	<a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>
    	<a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>
    </p>
</body>

<script type="text/javascript">
	var nodeid = "";
	mini.parse();
	var form = new mini.Form("form1");
	
	function SaveData() {
        var o = form.getData(true);            

        form.validate();
        if (form.isValid() == false) return;
        //验证输入的名称是否已经存在
        var tempName = mini.get("textbox1").getFormValue();
        $.post("medicine_sort_repeat_name.do",{name:tempName,pid:nodeid},function(data){
        	if(data.result){
        		mini.confirm("已在该类别下找到重复的类别名，确定继续添加？", "确定？",function (action) {
        			if (action == "ok") {
        				$.ajax({
                            url: "medicine_sort_save.do",
                			type: 'post',
                			dataType: 'json',
                            data: o,
                            cache: false,
                            success: function (text) {
                            	if(text.success){
                	            	window.CloseOwnerWindow("succ");
                	            }else{
                	            	mini.alert("数据错误，请重新添加");
                	            }
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                alert(jqXHR.responseText);
                                CloseWindow();
                            }
                        });
        			}
        		});
        	}else{
				$.ajax({
                    url: "medicine_sort_save.do",
        			type: 'post',
        			dataType: 'json',
                    data: o,
                    cache: false,
                    success: function (text) {
                    	if(text.success){
        	            	window.CloseOwnerWindow("succ");
        	            }else{
        	            	mini.alert("数据错误，请重新添加");
        	            }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                        CloseWindow();
                    }
                });
			}
        },"json");
    }
	
	function onOk(e) {
        SaveData();
    }
	
	function SetData(data){
	     data = mini.clone(data);
	     nodeid = data.id;
         mini.getbyName("pid").setValue(data.id);
	}
	
	function CloseWindow(action) {            
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
	
	function onCancel(e) {
        CloseWindow("cancel");
    }

   	function updateError(e) {
	    var id =e.sender.name + "_error";
	    var el = document.getElementById(id);
	    if (el) {
	        el.innerHTML =e.errorText;
	    }
   	}
   
   	function checkValidation(e) {                  
  		updateError(e);
   	}
	
</script>